package com.fr.report.core;

import com.fr.base.FRContext;
import com.fr.base.Inter;
import com.fr.base.core.FT;
import com.fr.base.core.list.IntList;
import com.fr.report.CellElement;
import com.fr.report.FloatElement;
import com.fr.report.Margin;
import com.fr.report.PageInfo;
import com.fr.report.PaperSetting;
import com.fr.report.PaperSize;
import com.fr.report.Report;
import com.fr.report.ReportPage;
import com.fr.report.ReportPageAttr;
import com.fr.report.ReportSettings;
import com.fr.report.cellElement.CellPageAttr;
import java.awt.Point;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

public class SheetPageGenerator
{
  private Report report;
  private FT repeatrowheader;
  private FT detailrows;
  private FT repeatrowfooter;
  private FT repeatcolumnheader;
  private FT detailcolumns;
  private FT repeatcolumnfooter;
  private DynamicValueList columnWidthList;
  private DynamicValueList rowHeightList;
  private IntList pageColumnIndexList = new IntList();
  private IntList pageRowIndexList = new IntList();
  private int pageCount = 0;
  private List workSheetPageLists = new ArrayList();
  private ReportSettings reportSettings;
  private PaperSetting setting;

  public SheetPageGenerator(Report paramReport, PaperSetting paramPaperSetting)
  {
    this.report = paramReport;
    this.reportSettings = ReportUtils.getReportSettings(paramReport);
    this.columnWidthList = ReportHelper.getColumnWidthList(paramReport);
    this.rowHeightList = ReportHelper.getRowHeightList(paramReport);
    this.setting = paramPaperSetting;
    dealWithPageInfor();
    generatePages();
  }

  public ReportPage[] getReportPages()
  {
    return ((ReportPage[])this.workSheetPageLists.toArray(new ReportPage[this.workSheetPageLists.size()]));
  }

  private void dealWithPageInfor()
  {
    int i;
    int j;
    int i5;
    if (this.setting == null)
    {
      i = 2147483647;
      j = 2147483647;
    }
    else
    {
      PaperSize localPaperSize = this.setting.getPaperSize();
      Margin localMargin = this.setting.getMargin();
      double d1 = localPaperSize.getWidth();
      double d2 = localPaperSize.getHeight();
      if (this.setting.getOrientation() == 1)
      {
        d1 = localPaperSize.getHeight();
        d2 = localPaperSize.getWidth();
      }
      i = (int)((d1 - localMargin.getLeft() - localMargin.getRight()) * 96.0D);
      j = (int)((d2 - localMargin.getTop() - localMargin.getBottom() - this.reportSettings.getHeaderHeight() - this.reportSettings.getFooterHeight()) * 96.0D);
    }
    if ((i <= 0) || (j <= 0))
      throw new RuntimeException("paperPaintWidth/paperPaintHeight is less than 0");
    int k = 0;
    int l = 0;
    Iterator localIterator1 = this.report.floatIterator();
    while (localIterator1.hasNext())
    {
      localObject = (FloatElement)localIterator1.next();
      Point[] arrayOfPoint = ReportHelper.calculateLastColumnAndRowOfFloatElement(this.report, (FloatElement)localObject);
      k = Math.max(k, arrayOfPoint[0].x + 1);
      l = Math.max(l, arrayOfPoint[0].y + 1);
    }
    k = Math.max(k, this.report.getColumnCount());
    l = Math.max(l, this.report.getRowCount());
    this.repeatrowheader = new FT(0, 0);
    this.detailrows = new FT(0, l);
    this.repeatrowfooter = new FT(l, l);
    this.repeatcolumnheader = new FT(0, 0);
    this.detailcolumns = new FT(0, k);
    this.repeatcolumnfooter = new FT(k, k);
    Object localObject = this.report.getReportPageAttr();
    if (localObject != null)
    {
      int i1 = ((ReportPageAttr)localObject).getRepeatHeaderRowFrom();
      int i2 = ((ReportPageAttr)localObject).getRepeatHeaderRowTo() + 1;
      if ((i1 >= 0) && (i2 >= 0))
      {
        this.repeatrowheader.setFrom(Math.min(i1, i2));
        this.repeatrowheader.setTo(Math.max(i1, i2));
      }
      int i3 = ((ReportPageAttr)localObject).getRepeatFooterRowFrom();
      int i4 = ((ReportPageAttr)localObject).getRepeatFooterRowTo() + 1;
      if ((i3 >= 0) && (i4 >= 0))
      {
        this.repeatrowfooter.setFrom(Math.min(i3, i4));
        this.repeatrowfooter.setTo(Math.max(i3, i4));
      }
      this.detailrows.setFrom(this.repeatrowheader.getTo());
      this.detailrows.setTo(this.repeatrowfooter.getFrom());
      i5 = ((ReportPageAttr)localObject).getRepeatHeaderColumnFrom();
      int i6 = ((ReportPageAttr)localObject).getRepeatHeaderColumnTo() + 1;
      if ((i5 >= 0) && (i6 >= 0))
      {
        this.repeatcolumnheader.setFrom(Math.min(i5, i6));
        this.repeatcolumnheader.setTo(Math.max(i5, i6));
      }
      i8 = ((ReportPageAttr)localObject).getRepeatFooterColumnFrom();
      i9 = ((ReportPageAttr)localObject).getRepeatFooterColumnTo() + 1;
      if ((i8 >= 0) && (i9 >= 0))
      {
        this.repeatcolumnfooter.setFrom(Math.min(i8, i9));
        this.repeatcolumnfooter.setTo(Math.max(i8, i9));
      }
      this.detailcolumns.setFrom(this.repeatcolumnheader.getTo());
      this.detailcolumns.setTo(this.repeatcolumnfooter.getFrom());
    }
    IntList localIntList1 = new IntList();
    IntList localIntList2 = new IntList();
    Iterator localIterator2 = this.report.cellIterator();
    while (true)
    {
      CellElement localCellElement;
      do
      {
        CellPageAttr localCellPageAttr;
        do
        {
          while (true)
          {
            if (!(localIterator2.hasNext()))
              break label822;
            localCellElement = (CellElement)localIterator2.next();
            localCellPageAttr = localCellElement.getCellPageAttr();
            if (localCellPageAttr != null)
              break;
          }
          if (localCellPageAttr.isPageAfterColumn())
            if ((i5 = localCellElement.getColumn() + localCellElement.getColumnSpan()) > 0)
              localIntList1.add(i5);
          if (localCellPageAttr.isPageBeforeColumn())
            if ((i5 = localCellElement.getColumn()) > 0)
              localIntList1.add(i5);
          if (localCellPageAttr.isPageAfterRow())
            if ((i5 = localCellElement.getRow() + localCellElement.getRowSpan()) > 0)
              localIntList2.add(i5);
        }
        while (!(localCellPageAttr.isPageBeforeRow()));
      }
      while ((i5 = localCellElement.getRow()) <= 0);
      localIntList2.add(i5);
    }
    label822: int i7 = this.columnWidthList.getRangeValue(this.repeatcolumnheader.getFrom(), this.repeatcolumnheader.getTo());
    int i8 = this.columnWidthList.getRangeValue(this.repeatcolumnfooter.getFrom(), this.repeatcolumnfooter.getTo());
    if (i7 + i8 >= i)
      throw new RuntimeException("repeated column width is larger than paper width.");
    this.pageColumnIndexList.add(0);
    int i9 = 0;
    int i10 = 0;
    for (int i11 = 0; i11 < this.detailcolumns.getFrom(); ++i11)
    {
      i9 += (i10 = this.columnWidthList.get(i11));
      if ((i9 > i) || (localIntList1.contain(i11)))
      {
        if (this.pageColumnIndexList.get(this.pageColumnIndexList.size() - 1) == i11)
          throw new DeathCycleException("Death cycle exist in paginating　dealing with columnIndex: " + i11);
        this.pageColumnIndexList.add(i11);
        i9 = i10;
      }
    }
    for (i11 = this.detailcolumns.getFrom(); i11 < this.detailcolumns.getTo(); ++i11)
    {
      i9 += (i10 = this.columnWidthList.get(i11));
      if ((i9 + i8 > i) || (localIntList1.contain(i11)))
      {
        if (this.pageColumnIndexList.get(this.pageColumnIndexList.size() - 1) == i11)
          throw new DeathCycleException("Death cycle exist in paginating　dealing with columnIndex: " + i11);
        this.pageColumnIndexList.add(i11);
        i9 = i7 + i10;
      }
    }
    for (i11 = this.detailcolumns.getTo(); i11 < k; ++i11)
    {
      i9 += (i10 = this.columnWidthList.get(i11));
      if ((i9 > i) || (localIntList1.contain(i11)))
      {
        if (this.pageColumnIndexList.get(this.pageColumnIndexList.size() - 1) == i11)
          throw new DeathCycleException("Death cycle exist in paginating　dealing with columnIndex: " + i11);
        this.pageColumnIndexList.add(i11);
        i9 = i10;
      }
    }
    this.pageColumnIndexList.add(k);
    i7 = this.rowHeightList.getRangeValue(this.repeatrowheader.getFrom(), this.repeatrowheader.getTo());
    i8 = this.rowHeightList.getRangeValue(this.repeatrowfooter.getFrom(), this.repeatrowfooter.getTo());
    if (i7 + i8 >= j)
      throw new RuntimeException("repeated row height is larger than paper height.");
    this.pageRowIndexList.add(0);
    i9 = 0;
    i10 = 0;
    for (i11 = 0; i11 < this.detailrows.getFrom(); ++i11)
    {
      i9 += (i10 = this.rowHeightList.get(i11));
      if ((i9 > j) || (localIntList2.contain(i11)))
      {
        if (this.pageRowIndexList.get(this.pageRowIndexList.size() - 1) == i11)
          throw new DeathCycleException("Death cycle exist in paginating　dealing with rowIndex: " + i11);
        this.pageRowIndexList.add(i11);
        i9 = i10;
      }
    }
    for (i11 = this.detailrows.getFrom(); i11 < this.detailrows.getTo(); ++i11)
    {
      i9 += (i10 = this.rowHeightList.get(i11));
      if ((i9 + i8 > j) || (localIntList2.contain(i11)))
      {
        if (this.pageRowIndexList.get(this.pageRowIndexList.size() - 1) == i11)
          throw new DeathCycleException("Death cycle exist in paginating　dealing with rowIndex: " + i11);
        this.pageRowIndexList.add(i11);
        i9 = i7 + i10;
      }
    }
    for (i11 = this.detailrows.getTo(); i11 < l; ++i11)
    {
      i9 += (i10 = this.rowHeightList.get(i11));
      if ((i9 > j) || (localIntList2.contain(i11)))
      {
        if (this.pageRowIndexList.get(this.pageRowIndexList.size() - 1) == i11)
          throw new DeathCycleException("Death cycle exist in paginating　dealing with rowIndex: " + i11);
        this.pageRowIndexList.add(i11);
        i9 = i10;
      }
    }
    this.pageRowIndexList.add(l);
    this.pageCount = ((this.pageColumnIndexList.size() - 1) * (this.pageRowIndexList.size() - 1));
  }

  private void generatePages()
  {
    int l;
    int i1;
    ArrayList localArrayList1;
    int i2;
    int i3;
    int i4;
    int i5;
    ArrayList localArrayList2;
    int i = 0;
    switch (this.reportSettings.getPageOrder())
    {
    case 0:
      j = 1;
      k = this.pageColumnIndexList.size();
      while (true)
      {
        if (j >= k)
          break label840;
        l = this.pageColumnIndexList.get(j - 1);
        i1 = this.pageColumnIndexList.get(j);
        localArrayList1 = new ArrayList();
        if ((l >= this.detailcolumns.getFrom()) && (l < this.detailcolumns.getTo()) && (this.repeatcolumnheader.getTo() > this.repeatcolumnheader.getFrom()))
          localArrayList1.add(new FT(this.repeatcolumnheader.getFrom(), this.repeatcolumnheader.getTo()));
        if (i1 > l)
          localArrayList1.add(new FT(l, i1));
        if ((i1 >= this.detailcolumns.getFrom()) && (i1 <= this.detailcolumns.getTo()) && (this.repeatcolumnfooter.getTo() > this.repeatcolumnfooter.getFrom()))
          localArrayList1.add(new FT(this.repeatcolumnfooter.getFrom(), this.repeatcolumnfooter.getTo()));
        i2 = 1;
        i3 = this.pageRowIndexList.size();
        while (i2 < i3)
        {
          i4 = this.pageRowIndexList.get(i2 - 1);
          i5 = this.pageRowIndexList.get(i2);
          localArrayList2 = new ArrayList();
          if ((i4 >= this.detailrows.getFrom()) && (i4 < this.detailrows.getTo()) && (this.repeatrowheader.getTo() > this.repeatrowheader.getFrom()))
            localArrayList2.add(this.repeatrowheader);
          if (i5 > i4)
            localArrayList2.add(new FT(i4, i5));
          if ((i5 >= this.detailrows.getFrom()) && (i5 <= this.detailrows.getTo()) && (this.repeatrowfooter.getTo() > this.repeatrowfooter.getFrom()))
            localArrayList2.add(this.repeatrowfooter);
          dealWithReportPage(localArrayList2, localArrayList1, i, j, i2);
          ++i;
          ++i2;
        }
        ++j;
      }
    }
    int j = 1;
    int k = this.pageRowIndexList.size();
    while (j < k)
    {
      l = this.pageRowIndexList.get(j - 1);
      i1 = this.pageRowIndexList.get(j);
      localArrayList1 = new ArrayList();
      if ((l >= this.detailrows.getFrom()) && (l < this.detailrows.getTo()) && (this.repeatrowheader.getTo() > this.repeatrowheader.getFrom()))
        localArrayList1.add(this.repeatrowheader);
      if (i1 > l)
        localArrayList1.add(new FT(l, i1));
      if ((i1 >= this.detailrows.getFrom()) && (i1 <= this.detailrows.getTo()) && (this.repeatrowfooter.getTo() > this.repeatrowfooter.getFrom()))
        localArrayList1.add(this.repeatrowfooter);
      i2 = 1;
      i3 = this.pageColumnIndexList.size();
      while (i2 < i3)
      {
        i4 = this.pageColumnIndexList.get(i2 - 1);
        i5 = this.pageColumnIndexList.get(i2);
        localArrayList2 = new ArrayList();
        if ((i4 >= this.detailcolumns.getFrom()) && (i4 < this.detailcolumns.getTo()) && (this.repeatcolumnheader.getTo() > this.repeatcolumnheader.getFrom()))
          localArrayList2.add(this.repeatcolumnheader);
        if (i5 > i4)
          localArrayList2.add(new FT(i4, i5));
        if ((i5 >= this.detailcolumns.getFrom()) && (i5 <= this.detailcolumns.getTo()) && (this.repeatcolumnfooter.getTo() > this.repeatcolumnfooter.getFrom()))
          localArrayList2.add(this.repeatcolumnfooter);
        dealWithReportPage(localArrayList1, localArrayList2, i, i2, j);
        ++i;
        ++i2;
      }
      ++j;
    }
    label840: FRContext.getLogger().log(Level.INFO, Inter.getLocText("INFO-Page_is_Generated"));
  }

  private void dealWithReportPage(List paramList1, List paramList2, int paramInt1, int paramInt2, int paramInt3)
  {
    ReportPage localReportPage = new ReportPage(this.report, new PageInfo(), this.setting, (FT[])paramList1.toArray(new FT[paramList1.size()]), (FT[])paramList2.toArray(new FT[paramList2.size()]), paramInt2, paramInt3);
    int i = ReportUtils.getReportSettings(this.report).getFirstPageNumber();
    int j = paramInt1 + i;
    int k = this.pageCount;
    localReportPage.setCurrentPageNumber(j);
    localReportPage.getPageInfo().setTotalPages(k);
    this.workSheetPageLists.add(localReportPage);
  }
}